package com.jiyinit.web.core.mobile.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.jiyinit.web.core.mobile.dto.ExchangeRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.web.core.mobile.dto.ExchangeCommentRequest;
import com.jiyinit.web.core.mobile.entity.TabCoreExchangeCommentEntity;
import com.jiyinit.web.core.mobile.service.ExchangeCommentServiceImpl;

import java.util.Date;

@Controller
@RequestMapping(value = "mobile/exchangeComment")
public class ExchangeCommentController {
	
	private static Logger logger = LoggerFactory.getLogger(ExchangeCommentController.class);
	
	@Resource(name = "core.exchangeCommentServiceImpl")
	private ExchangeCommentServiceImpl service;
	
	
	/**
	 * 分页查询
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPageRows")
	public PagingResult<TabCoreExchangeCommentEntity> getPageRows(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		ExchangeCommentRequest request = JsonEntityTransform.json2Object(request_json, ExchangeCommentRequest.class);
		logger.info("转换为obj之后的参数: {}", request.toString());
		request.setLimit(999);
		PagingResult<TabCoreExchangeCommentEntity> pag = service.findForPage(request);
		return pag;
	}

    /**
     * 帖子评论详情
     * @param req
     * @param request_json
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getHistoryList")
    public ResponseEntity<ResponseDto> getHistoryList(HttpServletRequest req, String request_json){
        logger.info("传入参数: {}", request_json);
        ExchangeCommentRequest request = JsonEntityTransform.json2Object(request_json, ExchangeCommentRequest.class);
        logger.info("转换为obj之后的参数: {}", request.toString());

        PagingResult<TabCoreExchangeCommentEntity> pag = service.historyList(request);
		return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(pag), HttpStatus.OK);
    }
	
	/**
	 * 添加评论内容
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> add(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreExchangeCommentEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreExchangeCommentEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());

        if(entity.getToUserId() != null && entity.getToUserId() != 0){
			entity.setAttr1(String.valueOf(entity.getId()));
			entity.setCreateTime(new Date());
			entity.setOptType(0);
			entity.setIsDelete(0);

            int i = 0;
            if(!Check.isNullObj(entity)){
                i = service.add(entity);
                logger.info("添加之后 的json串:{}", JsonEntityTransform.object2Json(entity));
            }
            if (i>0) {
                return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
            }
            return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);

        }else {
            entity.setCreateTime(new Date());
			entity.setToUserId(null);
            entity.setOptType(0);
            entity.setIsDelete(0);
            entity.setAttr1(null);
            int i = 0;
            if(!Check.isNullObj(entity)){
                i = service.add(entity);
                logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
            }
            if (i>0) {
                return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
            }
            return new ResponseEntity<>(ResponseDto.responseFail("添加失败"),HttpStatus.OK);
        }

	}

	/**
	 * 添加点赞
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAddPraise", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> addPraise(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreExchangeCommentEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreExchangeCommentEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());

		int num = service.find(entity);
		if(num>0){
            int i = 0;
            if(!Check.isNullObj(entity)){
                i = service.deleteByUserId(entity);
                logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
            }
            if (i>0) {
                return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
            }
            return new ResponseEntity<>(ResponseDto.responseFail("点赞错误"),HttpStatus.OK);
		}else {
			entity.setCreateTime(new Date());
			entity.setOptType(1);
			entity.setIsDelete(0);

			int i = 0;
			if(!Check.isNullObj(entity)){
				i = service.add(entity);
				logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
			}
			if (i>0) {
				return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
			}
			return new ResponseEntity<>(ResponseDto.responseFail("点赞失败"),HttpStatus.OK);
		}
	}

	
	/**
	 * 修改
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> update(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreExchangeCommentEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreExchangeCommentEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());
		int i = 0;
		if(!Check.isNullObj(entity)){
			i = service.update(entity);
			logger.info("更新之后的json串:{}", JsonEntityTransform.object2Json(entity));
		}
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> delete(HttpServletRequest req, String request_json) {
		logger.info("传入参数: {}", request_json);
		String str = req.getParameter("ids");
		int i = 0;
		if(!Check.isNullStrStrict(str)){
			String[] strs = str.split(",");
			for (int j = 0; j < strs.length; j++) {
				int id = Integer.valueOf(strs[i]);
				i += service.delete(id);
			}
		}
		
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}

}
